﻿<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comm_model extends CI_Model{
	
	public function __construct(){
		
		parent::__construct();
		
		$this->load->database();
	}
	
	/*
	 * 获取所有分类
	* */
	public function get_all_cate(){
		
		$query = $this->db->get('category');
		
		return $query->result_array();
	}
	
	
	/*
	 * 获取产品分类
	 * */
	public function get_category($pid = ''){
		
		if($pid){
			
			$query = $this->db->get_where('category',array('pid'=>$pid));
			
			return $query->result_array();
		}else{
			
			$query = $this->db->get_where('category',array('pid'=>'0'));
			
			return $query->result_array();
		}
		
	}
	
	/*
	 *获取主分类名称数组 
	 * */
	public function get_main_cate_name(){
		
		$res = $this->get_category();
		
		$data = array();
		
		foreach ($res as $key=>$val){
			
			$data[] = $val['cateurl'];
		}
		
		return $data;
		
	}
	
	
	/*
	 * 获取主分类下的子分类
	 * */
	public function get_childcates($cate_p=''){
		
		if($cate_p){
			
			$q = $this->db->get_where('category',array('cateurl'=>$cate_p));
			
			$re = $q->row_array();
			
			$qq = $this->db->get_where('category',array('pid'=>$re['id']));

			return $qq->result_array();
			
		}else{
			
			return false;
		}
	}
	
	/*
	 * 判断是否为分类
	 * */
	public function iscates($cate_p){
		
		if($cate_p){
			
			$q = $this->db->get_where('category',array('cateurl'=>$cate_p));
			
			return $this->db->affected_rows();
			
		}else{
			
			return false;
		}
		
	}
	
	
	
	
	/*
	 * 获取单个分类信息
	 * */
	public function get_cate_info($id = ''){
		
		if($id){
			
			$query = $this->db->get_where('category',array('id'=>$id));
			
			return $query->row_array();
		}else{
			
			return false;
		}
		
	}
	
	
	/*
	 * 获取单一指定分类下产品个数
	 * */
	public function get_cate_pro_num($cate_id = ''){
		
		if($cate_id){
			
			$query = $this->db->get_where('products',array('cate_id'=>$cate_id));
			
			return $query->num_rows();
		}
	}
	
	/*
	 * 获取多个指定分类信息（包含产品个数）
	 * */
	public function category_info($id = 0,&$data){
		
		$cate[$id] = $this->get_category($id);

		
		if($cate[$id]){
			
			$data[$id] = ($this->get_cate_info($id))?($this->get_cate_info($id)):'';

			$data[$id]['num'] = $this->get_cate_pro_num($id);
			
			foreach ($cate[$id] as $key=>$val){
				
				$data[$id]['num'] += $this->category_info($val['id'],$data);
					
			}
		}else{

			$data[$id] = ($this->get_cate_info($id))?($this->get_cate_info($id)):'';
			
			$data[$id]['num'] = $this->get_cate_pro_num($id);
			
		}
		
		return $data[$id]['num'];
		
	}
	
	/*
	 * 输出多个指定分类信息
	 * */
	public function print_category($pid = 0){
		
		$data = array();
		
		$this->category_info($pid,$data);
		
		unset($data[0]);

		foreach ($data as $key=>$val){
			
			if($val['pid'] != $pid){
				
				$data[$val['pid']]['child'][]=$val;
				
				unset($data[$key]);
			}
		}	
		
		return $data;
	}
	
	/*
	 * 根据cateurl获取分类信息
	 * */
	public function get_cates($cate_url = ''){
		
		if($cate_url){
			
			$query = $this->db->get_where('category',array('cateurl'=>$cate_url));
				
			return $query->row_array();

		}else{
			
			return false;
		}
	}
	
	/*
	 * 获取限定个数产品
	 * */
	public function get_limit_product($start=0,$end=12,$cate_id = ''){
		
		if(!$cate_id){
			
			$query = $this->db->get('products',$end,$start);
			
			$re = $query->result_array();
			
		}else{
			
			$cate_ids = array();
			
			$this->comm_model->get_cate_id($cate_id,$cate_ids);
			
			$sql = "select * from products where cate_id = ".$cate_id;
			
			foreach ($cate_ids as $key => $val){
				
				$sql .= "  or cate_id = '$val' ";
			};
			
			$sql .= " limit $start,$end";
			
			$query = $this->db->query($sql);
			
			$re = $query->result_array();
	
		}
		
		if(strstr($hosturl = $_SERVER['SERVER_NAME'],"watch")){
				
			$hosturl = str_replace('watch', 'http://images', $hosturl);
                        $hosturl = "http://images.meicii.com";
				
		}else{
				
			$hosturl = 'http://image.'.$hosturl;
		}
		
		foreach ($re as $key=>$val){
			
			$re[$key]['product_images'] = str_replace('/images', $hosturl.'/img180', $val['product_images'].'1.jpg');
		
		}
                /*
                $hosturl = 'http://www.ztproduct.com/';
		
		foreach ($re as $key=>$val){
			
			$re[$key]['product_images'] =  $hosturl.$val['product_images'].'1.jpg';
				
		}
                 * 
                 */
			
		return $re;
		
	}
	
	/*
	 * 获取分类id及其子分类id
	 * */
	public function get_cate_id($cate_id = '',&$cate_ids){
		
		if($cate_id){
			
			$query = $this->db->get_where('category',array('pid'=>$cate_id));
			
			$result = $query->result_array();
			
			if($result){
				
				foreach ($result as $key=>$val){
					
					$this->get_cate_id($val['id'],$cate_ids);
				}
			}
				
			$cate_ids[] = $cate_id; 

		}else{
			
			return false;
		}
	}
	
	
	/*
	 * 获取分类产品信息
	 * */
	public function get_cate_product($cate_url = '',$cate_pages = array(),&$total_pages){
		
		$res = $this->get_cates($cate_url);
		
		$cate_ids = array();
		
		$this->get_cate_id($res['id'],$cate_ids);
		
		$sql = "select * from products where cate_id = ".$res['id'];
		
		foreach ($cate_ids as $key=>$val){
			
			$sql .= "  or cate_id = '$val' ";	
		}
		
		$query = $this->db->query($sql);
		
		$num = $query->num_rows();
		//echo $num.";".$total_pages;
		$total_pages = $num%24==0?$num/24:intval($num/24)+1;
               // echo $num.";".$total_pages;
		
		$sql .= " limit ".$cate_pages['page']*$cate_pages['page_num'].",".$cate_pages['page_num'];
		
		$query = $this->db->query($sql);
		
		$re = $query->result_array();
		
                
		if(strstr($hosturl = $_SERVER['SERVER_NAME'],"watch")){
				
			//$hosturl = str_replace('watch', 'http://images', $hosturl);
                    $hosturl = "http://images.meicii.com";
				
		}else{
				
			$hosturl = 'http://image.'.$hosturl;
		}
		
		foreach ($re as $key=>$val){
			
			$re[$key]['product_images'] = str_replace('/images', $hosturl.'/img180', $val['product_images'].'1.jpg');
				
		}
                /*
                $hosturl = 'http://www.ztproduct.com/';
		
		foreach ($re as $key=>$val){
			
			$re[$key]['product_images'] =  $hosturl.$val['product_images'].'1.jpg';
				
		}*/
		
		return $re;
		
	}
	
	/*
	 * 获取当前产品信息
	 * */
	public function get_pro_info($product_url = ''){
		
		if($product_url){
			
			$query = $this->db->get_where('products',array('product_url'=>$product_url));
			
			$re =  $query->row_array();
			
			return $re;
			
		}else{
			
			return false;
		}
		
	}
	
	/*
	 * 获取当前产品的上级分类
	 * */
	public function get_pre_cate($product_url = ''){
		
		if($product_url){
			
			$data = array();
			
			$res = $this->get_pro_info($product_url);
			
			if(!$res){
				
				show_404();exit;
			}
			
			$cate_info = $this->get_cate_info($res['cate_id']);
			
			$data['cate_c_arr'] = $cate_info;
			
			if($cate_info['pid']==0){
				
				$data['cate_p_arr'] = $cate_info;
				
			}else{
				
				$cate_info = $this->get_cate_info($cate_info['pid']);
				
				$data['cate_p_arr'] = $cate_info;
			}
			
			return $data;
			
		}else{
			
			return false;
		}
	}
	
	
	/*
	 * 获取所有国家
	 * */
	public function get_countrys(){
		
		$query = $this->db->get('country');
		
		return $query->result_array();
	}
	
	/*
	 * 获取网站信息
	 * */
	public function get_site_info(){
		
		$query = $this->db->get_where('site_info',array('index_id'=>1));
		
		return $query->row_array();
	}
	
	
	/*
	 * 获取用户访问IP
	 * */
	public function get_onlineip() {

	    $onlineip = '';
	
	    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
	
	        $onlineip = getenv('HTTP_CLIENT_IP');
	
	    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
	
	        $onlineip = getenv('HTTP_X_FORWARDED_FOR');
	
	    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
	
	        $onlineip = getenv('REMOTE_ADDR');
	
	    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
	
	        $onlineip = $_SERVER['REMOTE_ADDR'];
	
	    }
	
	    return $onlineip;

	}
	
	/*
	 * 数据传输
	 * */
	public function vpost($url = '',$data = ''){
		
		$curl = curl_init();
		
		curl_setopt($curl, CURLOPT_URL, $url); 
		
		curl_setopt($curl, CURLOPT_POST, 1); 
		
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		
		curl_setopt($curl, CURLOPT_TIMEOUT, 30);
		
		curl_setopt($curl, CURLOPT_HEADER, 0);
		
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		
		$tmpInfo = curl_exec($curl);
		
		if (curl_errno($curl)) {
			
			echo 'Errno'.curl_error($curl);
			
		}
		
		curl_close($curl); 
		
		return $tmpInfo;
	}
	
	
	
	/*
	 * 添加产品到购物车
	* */
	public function insert_cart(){
	
		$all_seesion_data = $this->session->all_userdata();
		
		$post_info = $this->input->post();
		
		$query = $this->db->get_where('products',array('product_id'=>$post_info['product_id']));
		
		$pro_info = $query->row_array();
	
		$qty = $post_info['qty'];
		
		$product_key = $pro_info['product_id'];

		if(array_key_exists('product',$all_seesion_data)&&$all_seesion_data['product'] != NULL){
		
			foreach ($all_seesion_data['product'] as $key => $value){
		
				if ($key == $product_key){
						
					$qty += $value['qty'];
						
					break;
				}
					
				$qty = $qty;
		
			}
		}
			
		$data = array(
				'qty' => $qty,
				'product_id' => $product_key,
				'product_price' => $pro_info['product_price'],
				'product_name' => $pro_info['product_name'],
				'size' => $pro_info['psize'],
				'sku' => $pro_info['sku'],
				'url' => $pro_info['product_url'],
				'img' => $post_info['img']
		);

		if(array_key_exists('product',$all_seesion_data)){
				
			$data_add = $all_seesion_data['product'];
				
			$data_add[$product_key] = $data;
			
		}else{
				
			$data_add[$product_key] = $data;
		}
			
		$this->session->set_userdata('product',$data_add);
	
	}
	
	
	/*
	 * 删除购物车中某个产品
	* */
	public function delete(){
	
		$id = $this->input->get('id');
	
		$all_seesion_data = $this->session->all_userdata();
			
		$arr = $all_seesion_data['product'];
			
		unset($arr[$id]);
	
		$this->session->set_userdata('product',$arr);
	}
	
	/*
	 * 提交支付
	 * */
	public function do_pay($msg = array()){
		

		$key = 'DPRR29NV6YLE';		
		
		
		$signature = md5($msg['orderid'].$msg['siteid'].$key);
		
		$data = array(
				'siteno' => $msg['siteid'],
				'orderid' => $msg['orderid'],
				'currency' => $msg['currency'],
				'language' => 'en',
				'vat' => 0,
				'discount' => 0,
				'ShippingFee' => $msg['shippingfee'],
				'productno' => $productno,
				'shipfirstname' => $msg['ShippingFirstName'],
				'shiplastname' => $msg['ShippingLastName'],
				'shipaddr' => $msg['ShippingAddress'],
				'shipcity' => $msg['ShippingCity'],
				'shipstate' => $msg['ShippingState'],
				'shippostcode' => $msg['ShippingZipcode'],
				'shipcountry' => $msg['ShippingCountry'],
				'email' => $msg['email'],
				'shiptel' => $msg['ShippingTelephone'],
				'verifyCode' => $signature,
				'reUrl' => site_url().'user_mag',
				'notifyUrl' => site_url().'return_page'
		);
			
		$i = 0;
		foreach ($msg['product'] as $k=>$v){
			$data['productno['.$i.']'] = $v['product_id'];
			$data['productname['.$i.']'] = $v['product_name'];
			$data['priceunit['.$i.']'] = $v['product_price']*$msg['rate'];
			$data['quantity['.$i.']'] = $v['qty'];
			$i++;
		}
		
		return $data;
		 
	}
	
	public function GetPageContent($source_url, $data){
		$curl = curl_init($source_url); 
		
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); 
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 

		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

		curl_setopt($curl, CURLOPT_POST, 1); 
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
		
		curl_setopt($curl, CURLOPT_REFERER, "http://www.replicawatchebuy.com/ ");
		//curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:172.246.45.30', 'CLIENT-IP:172.246.45.30'));//IP
		
		$Result = curl_exec($curl); 
				
		curl_close($curl);
		
		return $Result;
	  }
	
	
	  
	  /*
	   * 获取货币信息
	   * */
	public function get_currency($currency = ''){
		
		if($currency){
			
			$aa =  $this->db->get_where('sdk_currency',array('currency'=>$currency));
			
		}else{
			
			$aa =  $this->db->get_where('sdk_currency',array('default_c'=>1));
		
		}
		
		return $aa->row();
		
	}
	
	public function pro_search(){
		
		$pro_name = $this->input->post('pro_name');
		
		$sql = "select * from products where product_name like '%".$pro_name."%' limit 0,4";
	
		$query = $this->db->query($sql);
		
		$re = $query->result_array();
		
		foreach ($re as $key=>$val){
				
			$img_arr = $this->get_pic($val['product_images'],$val['product_id']);
				
			if($img_arr){
		
				$re[$key]['product_images'] = $img_arr;
			}else{
		
				$re[$key]['product_images'][2] = '';
			}
		
		}
		
		return $re;
		
	}
	
	public function change_currency($the_data = ''){
		
		if($the_data){
			
			foreach ($the_data as $k=>$v){
				
				$sql = "update sdk_currency set default_c='$v->default_c',rate='$v->rate' where currency='$v->currency'";
				
				$this->db->query($sql);
			}
			
			return true;
			
		}else{
			
			return false;
		}
	}
	
	public function cartdelete($product_id = ''){
		
		if($product_id){
			
			$all_seesion_data = $this->session->all_userdata();
				
			$arr = $all_seesion_data['product'];
				
			unset($arr[$product_id]);
				
			$this->session->set_userdata('product',$arr);
			
			return true;
			
		}else{
			
			return FALSE;
		}
	}
	
	public function changeqty(){
		
		$product_id = $this->input->post('product_id');
		
		$qty = $this->input->post('qty');
		
		$all_seesion_data = $this->session->all_userdata();
			
		$data = $all_seesion_data['product'];
			
		$data[$product_id]['qty'] = $qty;
			
		$this->session->set_userdata('product',$data);
	}
	
	public function get_pic($imgurl = '',$product_id = ''){
		
		if(!$imgurl||!$product_id){
			
			return false;
		}
		
		$picnum = 0;
		
		$imgarr = array();
		
		if(strstr($hosturl = $_SERVER['SERVER_NAME'],"watch")){
				
			$hosturl = str_replace('watch', 'http://images', $hosturl);
                        $hosturl = "http://images.meicii.com";
				
		}else{
				
			$hosturl = 'http://image.'.$hosturl;
		}
			
		$imgdir = str_replace('images',$hosturl.'/img180',substr(trim($imgurl),1));
		
		for($i=1;;$i++){
			
			if(@file_get_contents($imgdir.$i.".jpg",0,null,0,1)){
				
				$imgarr[] = $imgdir.$i.".jpg";
				
				$picnum = $i;
				
			}else{
				
				if($i == 1){

					return false ;
					
				}
				
				break;
			}

		}
		
		return $imgarr;
		
		//$sql = "updata products set picnum = '$picnum' where product_id=".$product_id;
		
		//$this->db->query($sql);
		
		
	}
	public function get_pic1($imgurl = '',$product_id = ''){
	
		if(!$imgurl||!$product_id){
				
			return false;
		}
	
		$picnum = 0;
	
		$imgarr = array();
			
		$imgdir = 'http://www.ztproduct.com/'.substr(trim($imgurl),1);
	
		for($i=1;;$i++){
				
			if(@file_get_contents($imgdir.$i.".jpg",0,null,0,1)){
	
				$imgarr[] = $imgdir.$i.".jpg";
	
				$picnum = $i;
	
			}else{
	
				if($i == 1){
	
					return false ;
						
				}
	
				break;
			}
	
		}
	
		return $imgarr;
	
	}
	
	public function insert_wish($uid = '',$num = ''){
		
		if(!$uid||!$num) return false;
		
		$query = $this->db->get_where('wishlist',array('uid'=>$uid,'product_id'=>$num));
		
		if($query->row_array()){
			
			return false;
		}
		
		return $this->db->insert('wishlist',array('uid'=>$uid,'product_id'=>$num));
		
	}
	
	public function get_wish($uid = '',$page = '',&$total_page){
		
		if(!$uid) return false;
		
		$size = 5;
		
		//$query = $this->db->get('wishlist');
                $sql = "select * from wishlist where uid='$uid' ";
		
		$query = $this->db->query($sql);
		
		$totalpage = ceil(($query->num_rows())/$size);
		
		if(!$totalpage) return false;
		
		$sql = "select * from wishlist where uid='$uid' limit ".($page-1)*$size.",".$size;
		
		$query = $this->db->query($sql);
		
		$re = $query->result_array();
		
		if(!$re) return false;
		
		if(strstr($hosturl = $_SERVER['SERVER_NAME'],"watch")){
				
			$hosturl = str_replace('watch', 'http://images', $hosturl);
                        $hosturl = "http://images.meicii.com";
				
		}else{
				
			$hosturl = 'http://image.'.$hosturl;
		}
		
		foreach ($re as $key=>$val){
			
			$query = $this->db->get_where('products',array('product_id'=>$val['product_id']));
			
			$re[$key] = $query->row_array();
				
			$re[$key]['product_images'] = str_replace('/images', $hosturl.'/img180', $re[$key]['product_images'].'1.jpg');
		
		};
                /*
                $hosturl = 'http://www.ztproduct.com/';
		
		foreach ($re as $key=>$val){
			
			$re[$key]['product_images'] =  $hosturl.$val['product_images'].'1.jpg';
				
		}
		*/
		$total_page = $totalpage;
		
		return $re;
		
	}
	
	public function deletewish($uid='',$num = ''){
		
		if(!$uid||!$num) return false;
		
		return $this->db->delete('wishlist',array('uid'=>$uid,'product_id'=>$num));
		
	}
	
	
	public function change_token($token = ''){
		
		return $this->db->update('site_info',array('token'=>$token),array('index_id'=>1));
	}
	
	
	
	
	
	
	
	
	
	
	
}
